package main

import (
	"fmt"
	"net"
	"os"
)

func main() {
	//“连接”服务端
	conn, err := net.Dial("udp", "127.0.0.1:8090")
	if err != nil {
		fmt.Println("Dial err:", err)
		return
	}
	defer conn.Close()
	//启动goroutine，读取键盘输入并向服务端发送消息
	go func() {
		inBuf := make([]byte, 1024)
		for {
			n, err := os.Stdin.Read(inBuf)
			if err != nil {
				fmt.Println("os.Stdin.Read err:", err)
				return
			}
			conn.Write(inBuf[:n])
		}
	}()
	//主goroutine读取服务端发来的消息
	buf := make([]byte, 1024)
	for {
		n, err := conn.Read(buf)
		if err != nil {
			fmt.Println("Read err:", err)
			return
		}
		fmt.Println("服务器发来：", string(buf[:n]))
	}

}
